<template>
	<view class="rf-notice-detail">
		<view class="share">
			<rf-tag type="gray" size="small" tui-tag-class="tui-tag-share tui-size" shape="circleLeft">
				<button class="share-btn" open-type="share" :class="'text-' + themeColor.name" @tap.stop="share()">
					<text class="iconfont iconfenxiang"></text>
					<text class="tui-share-text tui-gray">分享</text>
				</button>
			</rf-tag>
		</view>

		<block v-if="type === '1'">
			<view class="banner" v-if="announceDetail.imgPath">
				<rf-image class="banner-img" :src="announceDetail.imgPath ? baseUrl + '/upload/' + announceDetail.imgPath : ''"></rf-image>
				<view class="banner-title in2line">{{ announceDetail.noticeTopic }}</view>
			</view>
			<view class="banner-title" v-if="!announceDetail.imgPath">{{ announceDetail.noticeTopic }}</view>
			<view class="article-meta" v-if="announceDetail.createTime">
				<text class="article-author">{{ announceDetail.creater }}</text>
				<text class="article-text">发布于</text>
				<text class="article-time">{{ announceDetail.createTime }}</text>
			</view>
			<view class="article-content">
				<rf-parser lazy-load :html="announceDetail.noticeContent | filterHtml"></rf-parser>
			</view>
			<rf-empty info="暂无公告详情" v-if="!announceDetail && !loading"></rf-empty>
		</block>
		<block v-if="type === '2'">
			<view class="banner" v-if="newDetail.imgPath">
				<rf-image class="banner-img" :src="newDetail.imgPath ? baseUrl + '/upload/' + newDetail.imgPath : ''"></rf-image>
				<view class="banner-title in2line">{{ newDetail.infoTitle }}</view>
			</view>
			<view class="banner-title" v-if="!newDetail.imgPath">{{ newDetail.infoTitle }}</view>
			<view class="article-meta" v-if="newDetail.createTime">
				<text class="article-author">{{ newDetail.creater }}</text>
				<text class="article-text">发布于</text>
				<text class="article-time">{{ newDetail.createTime }}</text>
			</view>
			<view class="article-content">
				<rf-parser lazy-load :html="newDetail.infoText | filterHtml"></rf-parser>
			</view>
			<rf-empty info="暂无新闻详情" v-if="!newDetail && !loading"></rf-empty>
		</block>
		<block v-if="type === '3'">
			<view class="banner" v-if="activityDetail.activityPic">
				<rf-image class="banner-img" :src="activityDetail.activityPic ? baseUrl + '/upload/' + activityDetail.activityPic : ''"></rf-image>
				<view class="banner-title in2line">{{ activityDetail.activityName }}</view>
			</view>
			<view class="banner-title" v-if="!activityDetail.activityPic">{{ activityDetail.activityName }}</view>
			<view class="article-meta" v-if="activityDetail.createTime">
				<text class="article-author">{{ announceDetail.createName }}</text>
				<text class="article-text">发布于</text>
				<text class="article-time">{{ activityDetail.createTime }}</text>
			</view>
			<view class="article-content">
				<rf-parser lazy-load :html="activityDetail.activityDetail | filterHtml"></rf-parser>
			</view>
			<rf-empty info="暂无活动详情" v-if="!activityDetail && !loading"></rf-empty>
		</block>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>
</template>

<script>
	import {
		notifyAnnounceView,
		getNewsInfo,
		getActivityInfo
	} from '@/api/basic';
	import moment from '@/common/moment';
	import iConfig from '@/config/index.config.js';
	String.prototype.replaceAll = function(s1, s2) {
		return this.replace(new RegExp(s1, 'gm'), s2);
	};

	export default {
		data() {
			return {
				announceDetail: {},
				id: undefined,
				loading: true,
				userInfo: {},
				type: '1',
				newDetail: {},
				activityDetail: {},
				baseUrl: this.$mConfig.baseUrl,
				currentUrl: null
			};
		},
		filters: {
			// 时间格式化
			time(val) {
				return val.substring(0, val.length - 2);
			},
			filterHtml(html) {
				if (html) {
					let replacePath = '';
					if (iConfig.ueditorPath.indexOf('static') > -1) {
						replacePath = `/${iConfig.ueditorPath}`
					} else {
						replacePath = `${iConfig.ueditorPath}`
					}
					html = html.replaceAll("\/sdcc_home_v4", iConfig.baseUrl); //如果部署在sdcc_sdjx下使用此方法
					//html = html.replaceAll("/ueditor",iConfig.baseUrl+'/ueditor');    //如果部署在ROOT下，使用此方法
					html = html.replaceAll(iConfig.ueditorPath, iConfig.baseUrl + replacePath);
				}
				return html;
			}
		},
		onLoad(event) {
			//console.log("!this.announceDetail: " + !this.announceDetail);
			//console.log("this.announceDetail: " + JSON.stringify(this.announceDetail));
			this.userInfo = uni.getStorageSync('userInfo');
			this.id = event.id;
			this.type = event.type;
			this.currentUrl = `${this.$mConfig.hostUrl}/pages/index/notice/detail?id=${this.id}&type=${this.type}`;
			this.getNotifyAnnounceView(event.id);
		},
		onShareAppMessage() {
			return {
				title: this.banner.title,
				path: `/pages/index/notice/detail?id=${this.id}`
			};
		},
		methods: {
			// 分享商品
			share() {
				// #ifdef H5
				if (this.$mPayment.isWechat()) {
					this.shareClass = 'show';
				} else {
					this.$mHelper.h5Copy(this.currentUrl);
				}
				// #endif
				// #ifdef APP-PLUS
				console.log("this.currentUrl: ", this.currentUrl);
				console.log("this.$mConstDataConfig.appName: ", this.$mConstDataConfig.appName);
				console.log("this.baseUrl: ", this.baseUrl + '/upload/' + this.announceDetail.imgPath);
				this.$mHelper.handleAppShare(this.currentUrl, this.$mConstDataConfig.appName, this.announceDetail.title, this.baseUrl +
					'/upload/' + this.announceDetail.imgPath);
				// #endif
			},
			// 获取通知列表
			async getNotifyAnnounceView(id) {
				let corpId = 10;
				if (this.userInfo.corpId) corpId = this.userInfo.corpId;
				let userId = this.userInfo && this.userInfo.userId;
				let url = `${notifyAnnounceView}`;
				if (this.type === '2') {
					url = `${getNewsInfo}`;
				} else if (this.type === '3') {
					url = `${getActivityInfo}`;
				}
				await this.$http
					.get(url, {
						docId: id,
						corpId: corpId,
						userId: userId
					})
					.then(r => {
						this.loading = false;
						console.log('r.data: ' + JSON.stringify(r.data));
						let title = '详情';
						if (this.type === '1') {
							title = r.data.noticeTopic;
							this.announceDetail = r.data;
						} else if (this.type === '2') {
							title = r.data.infoTitle;
							this.newDetail = r.data;
						} else if (this.type == '3') {
							title = r.data.activityName;
							this.activityDetail = r.data;
						}
						uni.setNavigationBarTitle({
							title: title
						});
					})
					.catch(() => {
						this.loading = false;
					});
			}
		}
	};
</script>

<style lang="scss">
	.rf-notice-detail {
		.banner {
			overflow: hidden;
			position: relative;
			background-color: #ccc;

			.banner-img {
				height: 300upx;
				width: 100%;
			}

			.banner-title {
				max-height: 84upx;
				overflow: hidden;
				position: absolute;
				bottom: 0;
				width: 100%;
				font-size: 32upx;
				font-weight: 400;
				line-height: 42upx;
				color: white;
				z-index: 11;
				background-color: rgba(0, 0, 0, 0.25);
				padding: 4upx 20upx;
			}
		}

		.banner-title {
			padding: $spacing-lg $spacing-lg 0;
			font-size: $font-lg;
		}

		.article-meta {
			padding: 20upx 40upx;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			color: gray;

			.article-text {
				font-size: 26upx;
				line-height: 50upx;
				margin: 0 20upx;
			}

			.article-author,
			.article-time {
				font-size: 30upx;
			}
		}

		.article-content {
			padding: 0 30upx;
			overflow: hidden;
			font-size: 30upx;
			margin-bottom: 30upx;
		}
	}
</style>
